﻿using BLL;
using DAL;
using DataProj.Areas.BackSys.ServiceTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataProj.Areas.BackSys.Controllers
{
    public class WelcomeController : BaseController
    {

        public WelcomeController()
        {
                

        }
        // GET: BackSys/Welcome
        [HttpGet]
        public ActionResult Index()
        {
            
            //ViewBag.ReturnUrl = ReturnUrl;
            return View();
        }

        public ActionResult IndexPart()
        {
            //ViewBag.ReturnUrl = ReturnUrl;
            return PartialView();
        }
        [HttpGet]
        public ActionResult LogOut()
        {
            MySession.Abandon();
            ResetMQLink();

            return RedirectToAction("Login", "Home");
        }

        [HttpPost]
        public PartialViewResult ChangePage(string action,string controller)
        {
            //var action = Request["Action"].Trim().ToString();
            //var controller = Request["Controller"].Trim().ToString();
            return PartialView(action, controller);
        }

        [HttpPost]
        public ActionResult Update()
        {

            switch (LoginType)
            {
                case "用户":
                    UserInfo user = new UserInfo()
                    {
                        ID = Convert.ToInt32(Request["ID"].Trim()),
                        UserName = Request["UserName"],
                        UserPass = Request["UserPass"],
                        TrueName = Request["TrueName"],
                        Phone = Request["Phone"],
                        IDCard = Request["Phone"],
                        Email = Request["Phone"],
                        ContactAddress = Request["Address"],
                        UpdateTime = DateTime.Now
                    };
                    if (new UserinfoImpl().Update(user.ID, user)) return Json(1);
                    break;
                case "管理员":
                    Manager manager = new Manager()
                    {
                        ID = Convert.ToInt32(Request["ID"].Trim()),
                        UserName = Request["UserName"].Trim(),
                        UserPass = Request["UserPass"].Trim(),
                        TrueName = Request["TrueName"].Trim(),
                        Phone = Request["Phone"].Trim(),
                        IDCard = Request["Phone"].Trim(),
                        UpdateTime = DateTime.Now
                    };
                    if (new ManagerImpl().Update(manager.ID, manager)) return Json(1);
                    break;
                case "快递员":
                    Deliveryman deliveryman = new Deliveryman()
                    {
                        ID = Convert.ToInt32(Request["ID"].Trim()),
                        UserName = Request["UserName"].Trim(),
                        UserPass = Request["UserPass"].Trim(),
                        TrueName = Request["TrueName"].Trim(),
                        Phone = Request["Phone"].Trim(),
                        IDCard = Request["Phone"].Trim(),
                        UpdateTime = DateTime.Now
                    };
                    if (new DeliverymanImpl().Update(deliveryman.ID, deliveryman)) return Json(1);
                    break;
                default:
                    break;
            }
            return Json(0);

        }


        public ActionResult Profile()
        {
            if (string.IsNullOrEmpty(MySession.GetSession("loginType")))
            {
                return RedirectToAction("Login", "Home");
            }
            var bagImpl = new BagInfoImpl();
            var complaintImpl = new ComplaintBagImpl();

            switch (MySession.GetSession("loginType"))
            {
                case "管理员":
                case "快递员":
                    MySession.WriteSession("bagNums", bagImpl.GetList().Count);
                    MySession.WriteSession("complaintNums", complaintImpl.GetList().Count);
                    break;

                case "用户":
                    UserInfo user = MySession.GetSession_UserInfo("detail");
                    MySession.WriteSession("bagNums", bagImpl.GetList(user.UserName, user.Phone).Select(o=>o.Count).Sum());
                    MySession.WriteSession("complaintNums", complaintImpl.GetList(user.UserName, user.Phone).Count);
                    break;
                default:
                    break;
            }
            return PartialView();
        }
    }
}